# -*- coding: utf-8 -*-
from django.db import models
from django.dispatch import dispatcher
from djangotoolbox import fields

class Wave(models.Model):
    title =  models.CharField(verbose_name = 'Wavelet Title', max_length=512, default='<no title>')
    wave_id = models.CharField('Wave ID', max_length=64, unique = True)
    tags = fields.ListField( models.CharField(max_length=500), verbose_name ='Wavelet Tags', blank=True, null= True)

    def save(self, *args, **kwargs):
        self.title = self.title.replace('\t', '')
        super(Wave, self).save(*args, **kwargs)  

    class Meta:
        verbose_name = 'Wavelet'
        verbose_name_plural = 'Wavelets'
    
    def __unicode__(self):
        return self.title
    
class CatalogSection(models.Model):
    section_name = models.CharField(verbose_name = 'Section name', max_length = 128, blank = False, null = False)
    section_level = models.IntegerField(verbose_name = 'Section order', blank = False, null = False)

    def save(self, *args, **kwargs):
        self.section_name = self.section_name.replace('\t', '')
        super(CatalogSection, self).save(*args, **kwargs)  
    
    class Meta:
        verbose_name = 'Catalog section'
        verbose_name_plural = 'Catalog section'
        ordering = ('section_level',)
        
    def __unicode__(self):
        return "%s" % self.section_name
    
class Level(models.Model):
    catalog_section_id = models.ForeignKey(CatalogSection, verbose_name = 'Catalog section')
    level = models.IntegerField(verbose_name = "Level",  blank = False, null = False, primary_key = False)
    name = models.CharField(verbose_name = 'Level name', max_length=128)
    tags = models.TextField(max_length=500, verbose_name = "Level Tags")

    def save(self, *args, **kwargs):
        self.name = self.name.replace('\t', '')
        self.tags = self.tags.replace('\t', '')
        super(Level, self).save(*args, **kwargs)  
    
    class Meta:
        verbose_name = 'Level'
        verbose_name_plural = 'Levels'
        
    def __unicode__(self):
        return self.name

class Category(models.Model):
    wave_id = models.CharField('Category Wave ID', max_length=64)
    tags_or = models.TextField(max_length = 500, verbose_name = "Category tags joined by 'OR'", blank = True, null = True)
    tags_and = models.TextField(max_length = 500, verbose_name = "Category tags joined by 'AND'", blank = True, null = True) 
    header = models.CharField('Category header', max_length = 500)
    no_group = models.BooleanField('List of waves without user group', default = False, unique = False)
    
    def save(self, *args, **kwargs):
        self.header = self.header.replace('\t', '')
        self.tags_or = self.tags_or.replace('\t', '')
        self.tags_and = self.tags_and.replace('\t', '')
        super(Category, self).save(*args, **kwargs)      
    
    def delete(self):
        if not self.is_general:
            super(Category, self).delete()

    class Meta:
        verbose_name = 'Category'
        verbose_name_plural = 'Categories'
        
        
class ExtendedTag(models.Model):
    name = models.CharField(verbose_name = 'Tag name', max_length=128)
    tags = models.TextField(max_length=500, verbose_name = "Adding Tags")
    
    def save(self, *args, **kwargs):
        self.name = self.name.replace('\t', '')
        self.tags = self.tags.replace('\t', '')
        super(ExtendedTag, self).save(*args, **kwargs)      
    
    class Meta:
        verbose_name = 'Extended tag'
        verbose_name_plural = 'Extended tags'
        
    def __unicode__(self):
        return "%s: %s" % (self.name, self.tags)
    
class TagEditor(models.Model):
    email = models.CharField(verbose_name = 'User email', max_length = 60)
    #rights = fields.ListField(models.IntegerField(), verbose_name = 'User rights')
    
    class Meta:
        verbose_name = 'Tag editor'
        verbose_name_plural = 'Tag editors'
        
    def __unicode__(self):
        return "e-mail: %s" % (self.email)
    
class Operation(models.Model):
    action = models.CharField(verbose_name = 'Action', max_length=128)
    wave_id = models.CharField(verbose_name = 'Wave entry id', max_length=128)
    
    def save(self, *args, **kwargs):
        self.name = self.name.replace('\t', '')
        self.tags = self.tags.replace('\t', '')
        super(ExtendedTag, self).save(*args, **kwargs)      
    
    class Meta:
        verbose_name = 'Extended tag'
        verbose_name_plural = 'Extended tags'
        
    def __unicode__(self):
        return "%s: %s" % (self.name, self.tags)
    
